package com.iwinding.ladygg.ui.act.device;

import android.app.Activity;

import com.iwinding.ladygg.AppForegroundStateManager;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.bluetooth.bean.BleDevice;
import com.iwinding.ladygg.bluetooth.ibluetooth.IBleListener;
import com.iwinding.ladygg.bluetooth.impblekit.BleKitManager;
import com.iwinding.ladygg.bluetooth.impblekit.ICaneBleApi;
import com.iwinding.ladygg.ui.adapter.CommonAdapter;
import com.iwinding.ladygg.ui.view.ProDialogCreator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 82218 on 2017/8/29.
 */

public class MyBle implements IBleListener {

    private Activity mContext;

    private List<BleDevice> deviceList;
    private CommonAdapter<BleDevice> deviceAdapter;

    public MyBle(Activity mContext, List<BleDevice> deviceList, CommonAdapter<BleDevice> deviceAdapter) {
        this.mContext = mContext;
        this.deviceList = deviceList;
        this.deviceAdapter = deviceAdapter;
        if (null == deviceList) {
            this.deviceList = new ArrayList<>();
        }
        ProDialogCreator.getInstance().initContext(mContext);
    }

    public void initBle() {
//        BleKitManager.getInstance().initBle(mContext);
//        ICaneBleApi.getInstance().initContext(mContext);
//        ICaneBleApi.getInstance().setNeedAutoConnect(true);
//        BleKitManager.getInstance().setAutoConnBle(ICaneBleApi.getInstance());

        ICaneBleApi.getInstance().setNeedAutoConnect(true);
        BleKitManager.getInstance().setiBleListener(this);
        boolean iscon = BleKitManager.getInstance().isConnetBle();
        if (!iscon) {
            BleKitManager.getInstance().stopScanBle();
//            BleKitManager.getInstance().disConnetBle();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mContext.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (!AppForegroundStateManager.getInstance().isAppInForeground()) {
                                return;
                            }
                            ProDialogCreator.getInstance().showDialog(mContext.getResources().getString(R.string.scanning));
                        }
                    });
                    BleKitManager.getInstance().initBle(mContext);
                }
            }).start();
        } else {
            onScanFinish(BleKitManager.getInstance().getDeviceList());
        }
    }

    public void connetBle(BleDevice bleDevice) {
        ICaneBleApi.getInstance().setNeedAutoConnect(true);
        BleKitManager.getInstance().stopScanBle();
        if (BleKitManager.getInstance().isConnetBle()) {
            BleKitManager.getInstance().disConnetBle();
        }
        BleKitManager.getInstance().connetBle(bleDevice.deviceAddress);
    }

    public void distory() {
        ProDialogCreator.getInstance().dismissDialog();
        ICaneBleApi.getInstance().setNeedAutoConnect(true);
        BleKitManager.getInstance().setiBleListener(null);
    }


    @Override
    public void onStartScan() {
//        ProDialogCreator.getInstance().showDialog(mContext.getResources().getString(R.string.scanning));
        deviceAdapter.notifyDataSetChanged();
        ((DeviceActivity) mContext).setDeviceNum(deviceAdapter.getCount());
    }

    @Override
    public void onScanFinish(List<BleDevice> list) {
        ProDialogCreator.getInstance().dismissDialog();
        deviceList.clear();
        deviceList.addAll(list);
        deviceAdapter.notifyDataSetChanged();
        ((DeviceActivity) mContext).setDeviceNum(deviceAdapter.getCount());
    }

    @Override
    public void onConnected(BleDevice device) {
        deviceAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDisConnect(BleDevice device) {
        deviceAdapter.notifyDataSetChanged();
    }


}
